﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

/// <summary>
/// TreeHelperProduct 的摘要说明
/// </summary>
public class TreeHelperProduct
{
    public TreeHelperProduct()
    {

    }

    public static string generateTreeData()
    {
        DBHelper db = new DBHelper();
        DataTable dtType = db.getList("SELECT ID,name,parentID,isUsing,bh FROM [ProductionType] where isusing =1 ORDER BY ID ASC");

        List<TreeNodeProdyct> list = new List<TreeNodeProdyct>();

        foreach (DataRow dr in dtType.Rows)
        {
            TreeNodeProdyct node = dataRowToTreeNode(dr, dtType);
            list = addNode(node, list);
        }

        string s = Newtonsoft.Json.JsonConvert.SerializeObject(list).Replace(",\"nodes\":[]", "");

        return s;
    }

    private static List<TreeNodeProdyct> addNode(TreeNodeProdyct node, List<TreeNodeProdyct> list)
    {
        if (node.parentID == "0")//顶级节点
        {
            list.Add(node);
        }
        else
        {
            TreeNodeProdyct tmpNode = list.FirstOrDefault(n => n.tags == node.parentID);
            if (tmpNode != null)
            {
                list[list.IndexOf(tmpNode)].nodes.Add(node);
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].nodes.Count > 0)
                    {
                        list[i].nodes = addNode(node, list[i].nodes);
                    }
                }
            }
        }

        return list;
    }

    private static TreeNodeProdyct dataRowToTreeNode(DataRow dr, DataTable dt)
    {
        TreeNodeProdyct node = new TreeNodeProdyct();
        node.text = (dr["isUsing"].ToString() == "True" ? dr["name"].ToString() : dr["name"].ToString() + "(不可用)");

        if (dr["isUsing"].ToString() == "True")
        {
            node.color = getColor(dr["parentID"].ToString());
        }
        else
        {
            node.color = "red";
        }

        if (dr["parentID"].ToString() == "0")
        {
            node.selectable = "false";
        }
        else
        {
            node.selectable = "true";
        }
        node.tags = dr["ID"].ToString();
        node.parentID = dr["parentID"].ToString();
        node.bh= dr["bh"].ToString();

        return node;
    }

    private static string getColor(string typeID)
    {
        string color = "";

        switch (typeID)
        {
            case "0": color = "blue"; break;
            default: color = "purple"; break;
        }

        return color;
    }
}

public class TreeNodeProdyct
{
    public string text { get; set; }
    public string selectable { get; set; }

    public string color { get; set; }
    public string tags { get; set; }
    public string parentID { get; set; }

    public string bh { get; set; }
    public List<TreeNodeProdyct> nodes { get; set; }

    public TreeNodeProdyct()
    {
        nodes = new List<TreeNodeProdyct>();
    }
}